package com.asyf.provider.service.impl;

import com.asyf.provider.common.tcc.FirstAction;
import com.asyf.provider.common.tcc.SecondAction;
import com.asyf.provider.service.TccService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author yjz
 * @Date 2022/2/9 9:05
 */
@Service
@Slf4j
public class TccServiceImpl implements TccService {

    @Autowired
    private FirstAction firstAction;

    @Autowired
    private SecondAction secondAction;

    @Override
    @GlobalTransactional//分布式事务
    public String singleTest(int a) {

        //调用tcc事务方法
        firstAction.prepare(null, a);

        //模拟其他业务
        int b = 1;
        b = b / a;
        return "success";
    }

    @Override
    @GlobalTransactional//分布式事务
    public String demo2(int a) {
        //调用tcc事务方法
        firstAction.prepare(null, a);

        //模拟其他业务
        int b = 1;
        b = b / a;

        //mysql插入数据
        secondAction.prepare(null, a);
        return "success";
    }

}
